package com.aier.aop;

import com.aier.AierManager;
import com.aier.bean.common.CommonResult;
import com.aier.bean.common.GlobalCode;
import com.aier.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @filename: GlobalExceptionHandler
 * @description: 全局异常捕获
 * @author: market
 * @date: 2022/4/28 14:09
 */
@ControllerAdvice
@Component
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(AierManager.class);

    /**
     * @功能描述:全局捕捉自定义异常
     * @param e
     * @return: com.qrcode.common.entries.CommonResult
     * @author: market
     * @date: 2022/4/28 15:15
     */
    @ExceptionHandler(value = CustomException.class)
    @ResponseBody
    public CommonResult handleLoanerServiceException(CustomException e) {
        logger.error("自定义异常:" + e.getMessage());
        return new CommonResult(e.getCode(),e.getMessage());
    }


    /**
     * @功能描述:全局捕捉异常
     * @param req
     * @param resp
     * @param e
     * @return: com.qrcode.common.entries.CommonResult
     * @author: market
     * @date: 2022/4/28 15:15
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult handlePointerException(HttpServletRequest req, HttpServletResponse resp, Exception e) {
        e.printStackTrace();
        logger.error("全局异常:"+e.getMessage());
        return CommonResult.error(GlobalCode.REQUEST_FAIL.getCode(),GlobalCode.REQUEST_FAIL.getMessage());
    }
}